﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Chaos.messages
{
    class process_model_game : message
    {
        stream.streamable m_game = new model.game();
        public model.game Game
        {
            get { return m_game as model.game; }
            set { m_game = value; }
        }

        public override void process(context in_context)
        {
            ProcessModelGame e = new ProcessModelGame();
            e.Game = Game;
            System.Windows.Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Normal,
                new OnProcessModelGameDelegate(OnProcessModelGameUIThread), e);     
        }

        public override void serialize(stream.stream in_Stream)
        {
            in_Stream.begin("process_model_game")
                .serialize("game", ref m_game)
            .end();
        }

        public class ProcessModelGame : EventArgs
        {
            public model.game Game { get; set; }
            public ProcessModelGame()
            {
            }
        };
        public delegate void OnProcessModelGameDelegate(ProcessModelGame e);

        public void OnProcessModelGameUIThread(ProcessModelGame e)
        {
            model.game game = System.Windows.Application.Current.FindResource("model_game") as model.game;

            MemoryStream ms = new MemoryStream();
            stream.ostream_xml os = new stream.ostream_xml();
            os.open(ms);
            m_game.serialize(os);
            os.close();

            ms.Seek(0, SeekOrigin.Begin);
            messages.factory f = new messages.factory();
            stream.istream_xml xs = new stream.istream_xml(f);  
            xs.open(ms);
            game.serialize(xs);
            xs.close();
            ms.Close();
        }
    }
}
